﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading.Tasks;


namespace teavch.Util.Formatter.SourceCode
{

	internal class CascadedStyleSheet
	{

		public Regex _styleRegex = null;
		/*
		CSS parsing regex was taken from stackoverflow
		http://stackoverflow.com/questions/236979/parsing-css-by-regex/2694121#2694121 
		*/
		private const string CSSGroups = @"(?<stylename>(?:(?:[^,{]+),?)*?)\{(?:(?<attributename>[^}:]+):?(?<attributevalue>[^};]+);?)*?\}";
		private const string StyleNameKey = "stylename";
		private const string AttributeNameKey = "attributename";
		private const string AttributeValueKey = "attributevalue";
		private string _cssContent = "";

		public CascadedStyleSheet()
		{
			this._styleRegex = new Regex( CSSGroups, RegexOptions.IgnoreCase | RegexOptions.Compiled );
			this.Styles = new List<Style>();
		}

		public string CascadedStyleName { get; set; }

		public List<Style> Styles { get; set; }

		public void LoadCss( string CssContent )
		{
			this._cssContent = CssContent;
			List<KeyValuePair<string, List<KeyValuePair<string, string>>>> _list = new List<KeyValuePair<string, List<KeyValuePair<string, string>>>>();

			if ( !String.IsNullOrEmpty( this._cssContent ) )
			{
				MatchCollection MatchList = this._styleRegex.Matches( this._cssContent );
				Style style = null;
				foreach ( Match item in MatchList )
				{
					if ( item != null && item.Groups != null &&
								item.Groups[ StyleNameKey ] != null &&
								item.Groups[ StyleNameKey ].Captures != null &&
								item.Groups[ StyleNameKey ].Captures[ 0 ] != null &&
								!String.IsNullOrEmpty( item.Groups[ StyleNameKey ].Value ) )
					{
						style = new Style();
						style.Name = item.Groups[ StyleNameKey ].Captures[ 0 ].Value.Trim();
						style.StyleAttributes = new List<StyleAttribute>();
						StyleAttribute attribute = null;
						for ( int i = 0; i < item.Groups[ AttributeNameKey ].Captures.Count; i++ )
						{
							string className = item.Groups[ AttributeNameKey ].Captures[ i ].Value.Trim();
							string value = item.Groups[ AttributeValueKey ].Captures[ i ].Value.Trim();
							if ( !String.IsNullOrEmpty( className ) && !String.IsNullOrEmpty( value ) )
							{
								className = className.Trim();
								value = value.Trim();
								if ( !String.IsNullOrEmpty( className ) && !String.IsNullOrEmpty( value ) )
								{
									attribute = new StyleAttribute();
									attribute.AttributeName = className;
									attribute.AttributeValue = value;
									style.StyleAttributes.Add( attribute );
								}
							}
						}
						this.Styles.Add( style );
					}
				}
			}
		}

		public void Load( string cssFile )
		{
			if ( !File.Exists( cssFile ) )
				throw new FileNotFoundException( "Css file not found" );
			string sContent = File.ReadAllText( cssFile );
			this.LoadCss( sContent );
		}

	}

	internal class Style
	{
		public enum StyleType
		{
			Class,
			ElementID
		}
		public StyleType Type { get; set; }
		public string Name { get; set; }
		public List<StyleAttribute> StyleAttributes { get; set; }
	}

	internal class StyleAttribute
	{
		public string AttributeName { get; set; }
		public string AttributeValue { get; set; }
	}

}
